﻿using System;
using System.Collections;
using System.Collections.Generic;
using Meqs.Entity;
using Meqs.Logic.Interface;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;

namespace Meqs.Logic.Implement
{
    public class SchPremLogic : CommonLogic<SchPrem, int>, ISchPremLogic
    {

        #region Implementation of ICommonLogic<SchPrem, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(SchPrem entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(SchPrem entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public SchPrem FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<SchPrem> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<SchPrem> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        /// <summary>
        /// 添加數據
        /// </summary>
        /// <param name="cause"></param>
        [Transaction(TransactionMode.Requires)]
        public void InsertSchPrem(List<SchPrem> schPremList)
        {
            try
            {
                foreach (SchPrem schPrem in schPremList)
                {
                    InsertCustomer("SchPrem.Insert", schPrem);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 更新數據
        /// </summary>
        /// <param name="cause"></param>
        [Transaction(TransactionMode.Requires)]
        public void UpdateSchPrem(int uid, List<SchPrem> schPremList)
        {
            try
            {
                DeleteObject(uid);

                foreach (SchPrem schPrem in schPremList)
                {
                    InsertCustomer("SchPrem.Insert", schPrem);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 批量更新數據
        /// </summary>
        /// <param name="cause"></param>
        [Transaction(TransactionMode.Requires)]
        public void BatchUpdateSchPrem(List<int> listUserId, List<int> premList)
        {
            try
            {
                foreach (int uid in listUserId)
                {
                    List<SchPrem> schPremList = new List<SchPrem>();
                    foreach (int premNo in premList)
                    {
                        SchPrem schPrem = new SchPrem(uid, premNo);
                        schPremList.Add(schPrem);
                    }

                    UpdateSchPrem(uid, schPremList);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            } 
        }
    }
}
